package cs275.ants.shared;

import java.util.Set;
import java.util.Stack;
import java.util.HashSet;

public class CycleFreeStack<E> extends Stack<E> {
	
	private static final long serialVersionUID = 4686022161718924236L;
	Set<E> elementSet = new HashSet<E>();
	
	public E push (E item) { 

		if (!elementSet.contains(item)) { 
			elementSet.add(item);
		} else {
			while (!this.isEmpty() && this.pop() != item) {}
			// reset the set 
			elementSet = new HashSet<E>();
			elementSet.addAll(this);
		}
		
		return super.push(item);
	}
	

}
